<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: dzf_base_application.php 708 2013-05-29 11:29:22Z laoguozhang $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

/**
 * Description of dzf_base_application
 *
 * @author bilicen
 */
class dzf_base_application extends dzf_base_class
{

	public $appname = '';
	/**
	 * 路由
	 * @var dzf_base_router 
	 */
	public $router = null;
	public $controller = null;
	
	/**
	 * 模板引擎
	 * @var dzf_base_view
	 */
	public $view_engine = 'dzf_base_view';
	public $ip = '';

	protected $request;

	/**
	 * 预处理的调用
	 */
	public function __construct($config = array()) {
		$this->array_merge($this, $config);
		$this->ip = $this->_get_client_ip();
		$this->init();
	}

	/**
	 * from Yii
	 * @param mixed $v1
	 * @param mixed $v2
	 * @return mixed
	 */
	static public function array_merge($v1, $v2) {
		$args = func_get_args();
		$res = array_shift($args);
		while(!empty($args)) {
			$next = array_shift($args);
			foreach($next as $k => $v) {
				if(is_integer($k)) {
					isset($res[$k]) ? $res[] = $v : $res[$k] = $v;
				} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
					$res[$k] = self::array_merge($res[$k], $v);
				} else {
					$res[$k] = $v;
				}
			}
		}
		return $res;
	}

	public function run() {
		register_shutdown_function(array($this,'end'));
		$this->_dispatcher();
	}

	public function end() {
		//do something
	}

	protected function _dispatcher() {
		$this->run_controller($this->_router());
	}

	protected function _router() {
		if($this->router === null) {
			$this->router = new dzf_base_router();
		}
		$this->router->parse();
		return $this->router;
	}

	public function run_controller($router) {
		if($router->controller) {
			$cname = $this->appname.'_control_'.$router->controller;
			if(class_exists($cname) && ($this->controller = new $cname())) {
				$this->controller->run($router->action);
			} else {
				throw new Exception('The controller "'.$cname.'" is not exists.');
			}
		} else {
			throw new Exception('The router is not error.');
		}
	}

	public function init_view($controller = '') {
		$cname = $this->view_engine;
		return new $cname($controller = '');
	}

	public function init() {

	}

	public function request() {
		if(!isset($this->request)) {
			$this->request = new dzf_base_request();
		}
		return $this->request;
	}
	//临时调试通用
	public function debug($var = null, $vardump = false) {
		echo '<pre>';
		$vardump = empty($var) ? true : $vardump;
		if($vardump) {
			var_dump($var);
		} else {
			print_r($var);
		}
		exit();
	}
	
	protected function _get_client_ip() {
		//#start //模拟IP地址
		/*
		//dsetcookie('rips', '0'); //需要先种一cookie，然后再注释掉，以免影响后台登录
		if($_COOKIE['rips']) {
			$ips = array('121.9.243.123', '121.9.243.124', '121.9.243.125', '121.9.243.126',
						'121.9.243.127', '121.9.243.128', '121.9.243.129', '121.9.243.130',
					);
			return $ips[array_rand($ips)];
		}
		*/
		//#end
		$ip = $_SERVER['REMOTE_ADDR'];
		
		if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
			echo 2;
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
			echo 3;
			foreach ($matches[0] AS $xip) {
				if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
					$ip = $xip;
					break;
				}
			}
			echo 4;
		}
		return $ip;
	}
}